# $Id: libgame.py 1.1 2004/12/30 03:56:21 jimb Exp $
# Python game library.
# Author:   Jim Brooks  http://www.jimbrooks.org
# Date:     2004/08
# License:  GNU General Public License (GPL)
# Requires: Python 2.3, PyGame, SDL.
# ==============================================================================

import pygame
from pygame.locals import *

# Return true if one object has collided into another.
def Collided( obj1, obj2 ):
    if obj1.valid and obj2.valid:
        return obj1.rect.colliderect( obj2.rect )
    else:
        return False

# ------------------------------------------------------------------------------
# Text messages using PyGame fonts.
# ------------------------------------------------------------------------------

# Every message is a tuple consisting of (text,x,y,rgb,center).
# Add() adds message(s) to the message list.
# Del() removes matching message(s) from the list.
# Render() will display all message(s) in the list.

class Msg:

    def __init__( self, screen, font ):
        self.messages = []
        self.screen = screen # PyGame screen object
        self.font = font     # PyGame font object
        return

    def Add( self, msgTuples ):
        for m in msgTuples:
            self.messages.append( m )
        return

    def Del( self, msgTuples ):
        # For every message tuple to delete.
        for mdel in msgTuples:
            # For every message in messages[], delete it if it matches.
            tmpList = self.messages[:]
            i = -1
            for m in tmpList:
                i += 1
                if m == mdel:
                    del self.messages[i]
                    i -= 1
        return

    # Show a message immediately (won't be linked into list).
    def Show( self, (text,x,y,rgb,center) ):
        if pygame.font:
            f = self.font.render( text, 1, rgb )
            r = f.get_rect()
            if center:
                r.centerx = x
                r.centery = y
            else:
                r.left = x
                r.top  = y
            self.screen.blit( f, r )
        return

    def Render( self ):
        if pygame.font:
            for msg in self.messages:
                self.Show( msg )
        return
